/*
 * Copyright By ZATI
 * Copyright By 3a3c88295d37870dfd3b25056092d1a9209824b256c341f2cdc296437f671617
 * All rights reserved.
 *
 * If you are not the intended user, you are hereby notified that any use, disclosure, copying, printing, forwarding or
 * dissemination of this property is strictly prohibited. If you have got this file in error, delete it from your system.
 */

package cn.wzc.crystal.security.helper;

import cn.wzc.crystal.security.vo.LoginInfo;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 用户权限相关辅助类
 */
public abstract class SecurityHelper {

	/**
	 * 获取用户登录信息
	 *
	 * @return 当前登录用户信息, 如果没有登录/登录信息不正确,返回null
	 */
	public static LoginInfo getLoginUserInfo() {
		// 获取登录授权信息
		final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

		// 没有授权信息
		if (null == authentication) {
			return null;
		}
		// 未授权
		if (!authentication.isAuthenticated()) {
			return null;
		}

		// 获取登录信息
		if (authentication instanceof UsernamePasswordAuthenticationToken) {
			return (LoginInfo) authentication.getDetails();
		}

		return null;
	}


}
